package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    @Autowired
    private ICustomerTransferService customerTransferService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"移交历史列表权限", "customerTransfer:list"}, logical = Logical.OR)
    public String list(@ModelAttribute("qo") CustomerTransferQueryObject qo, Model model) {


        //获取当前用户信息  判断是否是销售经理或超级管理员
        Employee currentEmp = (Employee) SecurityUtils.getSubject().getPrincipal();
        //不是超级管理员 或 不是市场经理
        if (!(currentEmp.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))) {
            //此时用户信息为普通职员 看自己的移交历史信息 将自己的id作为动态sql查询
            qo.setSellerId(currentEmp.getId());
        }
        PageInfo<CustomerTransfer> pageInfo = customerTransferService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询所有才销售人员
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers", sellers);

        return "customerTransfer/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"移交历史编辑或新增权限", "customerTransfer:saveOrUpdate"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(CustomerTransfer customerTransfer, Model model) {
        JsonResult result = new JsonResult();
        try {
            customerTransferService.saveOrUpdate(customerTransfer);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

    //客户池吸纳
    @RequestMapping("/absorb")
    @ResponseBody
    @RequiresPermissions(value = {"客户池吸纳权限", "customerTransfer:absorb"}, logical = Logical.OR)
    public JsonResult absorb(CustomerTransfer customerTransfer) {
        JsonResult result = new JsonResult();
        try {
            //1.将客户吸纳改变为自己的客户  a.要获取当前登录的用户信息 b.修改客户的销售人员
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            customerService.updateSellerIdByCustomerId(employee.getId(),customerTransfer.getCustomer().getId());
            //更新客户的状态  由资源池客户变为潜在客户
            customerService.updateStatus(customerTransfer.getCustomer().getId(),Customer.STATUS_POTENTIAL);
            //吸纳产生移交历史
            customerTransferService.saveOrUpdate(customerTransfer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    //客户池移交
    @RequestMapping("/save")
    @ResponseBody
    @RequiresPermissions(value = {"客户池移交权限", "customerTransfer:save"}, logical = Logical.OR)
    public JsonResult save(CustomerTransfer customerTransfer) {
        JsonResult result = new JsonResult();
        try {
            customerTransferService.saveOrUpdate(customerTransfer);
            //客户池客户移交需更新客户的状态
            customerService.updateStatus(customerTransfer.getCustomer().getId(), Customer.STATUS_POTENTIAL);
            //修改客户的销售人员信息
            customerService.updateSellerIdByCustomerId(customerTransfer.getNewSeller().getId(), customerTransfer.getCustomer().getId());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }


}
